$(function() {

	submitDatas({
		type: 'post',//提交类型
		url: 'page/login',//提交地址
		async: false//是否异步
	});
	
	loadInputName({
		name: 'userPhone',//账号字段名
		password: 'password'//密码字段名
	});

	setInterval(carousel, 2500);

	submitBtn();

	verifyDiv();

	drawPic();

	$(".ant-dropdown-trigger").hover(ant_dropdown_trigger_hoverS, ant_dropdown_trigger_hoverH);

	$(".ant-dropdown-trigger-hover").hover(ant_dropdown_trigger_hoverS, ant_dropdown_trigger_hoverH);

	$(".qr-wrap img").eq(0).hover(weixinImgS, weixinImgH);

	$(".qr-wrap img").eq(1).hover(sAppImgS, sAppImgH);

	$(".weixinImg").hover(weixinImgS, weixinImgH);

	$(".sAppImg").hover(sAppImgS, sAppImgH);

	$(".ant-input").eq(0).blur(verifyDiv);

	$(".ant-input").eq(0).focus(input0);

	$(".ant-input").eq(1).focus(input1);

	$(".verifyInput").focus(input2);

	$(".picCode").click(verifyDiv);

	$(".ant-input").eq(0).keydown(submitBtn);

	$(".ant-input").eq(1).keyup(submitBtn);

	$(".verifyInput").keyup(verifyLength);

	$("#changeImg").click(verifyImg);

	$(".login-btn").click(function() {
		login();
		return false;
	});

});

function loadInputName(data){
	$(".ant-input").eq(0).attr("name",data.name);
	$(".ant-input").eq(1).attr("name",data.password);
}

var submitData;

function submitDatas(data) {
	submitData = data;
}

/** 生成一个随机数* */
function randomNum(min, max) {
	return Math.floor(Math.random() * (max - min) + min);
}
/** 生成一个随机色* */
function randomColor(min, max) {
	var r = randomNum(min, max);
	var g = randomNum(min, max);
	var b = randomNum(min, max);
	return "rgb(" + r + "," + g + "," + b + ")";
}
/** 绘制验证码图片* */
var codes = "";

function drawPic() {
	var canvas = document.getElementById("canvas");
	var width = canvas.width;
	var height = canvas.height;
	// 获取该canvas的2D绘图环境
	var ctx = canvas.getContext('2d');
	ctx.textBaseline = 'bottom';
	/** 绘制背景色* */
	ctx.fillStyle = randomColor(180, 240);
	// 颜色若太深可能导致看不清
	ctx.fillRect(0, 0, width, height);
	/** 绘制文字* */
	var str = 'abcdefghijkmnqprstuvwxyz0123456789';

	codes = "";

	// 生成四个验证码
	for(var i = 1; i <= 4; i++) {
		var txt = str[randomNum(0, str.length)];
		codes = codes + txt;
		ctx.fillStyle = randomColor(50, 160);
		// 随机生成字体颜色
		ctx.font = randomNum(15, 40) + 'px SimHei';
		// 随机生成字体大小
		var x = 10 + i * 25;
		var y = randomNum(25, 35);
		var deg = randomNum(-45, 45);
		// 修改坐标原点和旋转角度
		ctx.translate(x, y);
		ctx.rotate(deg * Math.PI / 180);
		ctx.fillText(txt, 0, 0);
		// 恢复坐标原点和旋转角度
		ctx.rotate(-deg * Math.PI / 180);
		ctx.translate(-x, -y);
	}

	/** 绘制干扰线* */
	for(var i = 0; i < 3; i++) {
		ctx.strokeStyle = randomColor(40, 180);
		ctx.beginPath();
		ctx.moveTo(randomNum(0, width / 2), randomNum(0, height / 2));
		ctx.lineTo(randomNum(0, width / 2), randomNum(0, height));
		ctx.stroke();
	}
	/** 绘制干扰点* */
	for(var i = 0; i < 50; i++) {
		ctx.fillStyle = randomColor(255);
		ctx.beginPath();
		ctx.arc(randomNum(0, width), randomNum(0, height), 1, 0, 2 * Math.PI);
		ctx.fill();
	} //console.log("1 "+codes);
	return codes;
}

var i = 4;
var carousel = function(e = 4) {
	if(i > 0) {
		e = e - i;
		$(".slick-dots li").attr("class", "");
		$(".slick-dots li").eq(e).attr("class", "slick-active");
		$(".slick-track").attr("class", "slick-track slick-track" + e);
		i--;
	} else {
		e = e - i;
		$(".slick-dots li").attr("class", "");
		$(".slick-dots li").eq(e).attr("class", "slick-active");
		$(".slick-track").attr("class", "slick-track slick-track" + e);
		i = 4;
	}
}

var ant_dropdown_trigger_hoverS = function() {
	$(".ant-dropdown-trigger-hover").show();
}

var ant_dropdown_trigger_hoverH = function() {
	$(".ant-dropdown-trigger-hover").hide();
}

var weixinImgS = function() {
	$(".weixinImg").show();
}

var weixinImgH = function() {
	$(".weixinImg").hide();
}

var sAppImgS = function() {
	$(".sAppImg").show();
}

var sAppImgH = function() {
	$(".sAppImg").hide();
}

var verifyDiv = function() {
	if($(".ant-input").eq(0).val().length != 0) {
		$(".kaptcha").slideDown();
	}
}

// 登录请求方法
var login = function() {
	// alert("发送登录请求")
	if($(".verifyInput").val() == codes) {
		var formData = {};
		$.map($("#loginForm").serializeArray(), function(object, index) {
			formData[object['name']] = object['value'];
		});
		$.ajax({
			type: submitData.type,
			url: submitData.url,
			async: submitData.async,
			data: formData,
			success: function(data) {
				if(typeof(data.url) != 'undefined') {
					window.location.href=data.url;
				} else {
					$(".ant-form-item-control").removeClass("has-error");
					$(".ant-form-item-control").eq(data.err).addClass("has-error");
				}
			},
			error: function() {
				alert("请求失败！");
			}
		});
	} else {
		drawPic();
		$(".ant-form-item-control").eq(2).addClass("has-error");
	}
}

var input0 = function() {
	$(".ant-form-item-control").eq(0).removeClass("has-error");
}

var input1 = function() {
	$(".ant-form-item-control").eq(1).removeClass("has-error");
	verifyDiv();
}

var input2 = function() {
	$(".ant-form-item-control").eq(2).removeClass("has-error");
}

var submitBtn = function() {
	if($(".ant-input").eq(0).val().length != 0 && $(".ant-input").eq(1).val().length != 0 && $(".verifyInput").val().length == 4) {
		$(".login-btn").removeAttr("disabled");
	} else {
		$(".login-btn").attr("disabled", "disabled");
	}
}

var verifyLength = function() { //console.log("2 "+codes);
	if($(".verifyInput").val().length == 4) {
		submitBtn();
	}
}

function verifyImg() {
	drawPic();
	$(".ant-form-item-control").eq(2).removeClass("has-error");
	//$(".verifyInput").val("");
	//$(".login-btn").attr("disabled","disabled");
}